package com.jmu.medicineknows.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jmu.medicineknows.dto.NewsDto;
import com.jmu.medicineknows.entity.NewsInfo;
import com.jmu.medicineknows.entity.Result;
import com.jmu.medicineknows.mapper.NewsMapper;
import com.jmu.medicineknows.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author ACER
 */
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, NewsInfo> implements NewsService {

    @Autowired
    NewsMapper newsMapper;
    @Override
    public Result newsList(NewsDto newsDto) {
        Page<NewsInfo> page = new Page<>(newsDto.getCurrentPage(),newsDto.getPageSize());
        LambdaQueryWrapper<NewsInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NewsInfo::getCategory,newsDto.getCategory());
        Page<NewsInfo> newsInfoPage = this.page(page, queryWrapper);
        return Result.defaultSuccess(newsInfoPage);
    }

    @Override
    public Result insertNews(NewsInfo newsInfo){
        if(newsMapper.insert(newsInfo)>0){
            return Result.defaultSuccess("insert success!");
        }else {
            return Result.defaultFailure();
        }

    }

    @Override
    public Result deleteNews(NewsInfo newsInfo) {
        if(newsMapper.deleteById(newsInfo.getId())>0){
            return Result.defaultSuccess("delete success!");
        }else {
            return Result.defaultFailure();
        }
    }

    @Override
    public Result updateNews(NewsInfo newsInfo) {
        if(newsMapper.updateById(newsInfo)>0){
            return Result.defaultSuccess("update success!");
        }else {
            return Result.defaultFailure();
        }
    }


    @Override
    public Result selectNews(NewsDto newsDto) {
        Page<NewsInfo> page = new Page<>(newsDto.getCurrentPage(),newsDto.getPageSize());
        LambdaQueryWrapper<NewsInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(0 != newsDto.getCategory(),NewsInfo::getCategory,newsDto.getCategory())
                .like(""!= newsDto.getTitle(),NewsInfo::getTitle,newsDto.getTitle());;
        Page<NewsInfo> newsInfoPage = this.page(page, queryWrapper);
        return Result.defaultSuccess(newsInfoPage);
    }
}
